<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery"
                  size="mini"/>
      </el-form-item>
      <el-form-item label="发布日期" prop="publishDate">
        <el-date-picker clearable v-model="queryParams.publishDate" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择发布日期" size="mini"></el-date-picker>
      </el-form-item>
      <el-form-item label="省级" prop="source">
        <el-select
            v-model="queryParams.source"
            placeholder="省级"
            clearable
            style="width: 240px"
        >
          <el-option
              v-for="dict in provincial_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="市级" prop="municipal_level">
        <el-select
            v-model="queryParams.municipal_level"
            placeholder="市级"
            clearable
            style="width: 240px"
        >
          <el-option
              v-for="dict in municipal_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否重点关注" prop="isImportant">
        <el-select v-model="queryParams.isImportant" placeholder="请选择是否重点关注" clearable @change="handleQuery"
                   size="mini">
          <el-option label="是" :value="1"></el-option>
          <el-option label="否" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
        <el-button size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!--操作栏-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:documents:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            @click="handleExport"
            v-hasPermi="['system:documents:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            @click="queryPolicy"
        >获取数据
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="documentsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="标题" align="center" prop="title" width="700">
        <template v-slot="{row}">
          《{{ row.title }}》
        </template>
      </el-table-column>
      <el-table-column label="链接" align="center" prop="href" width="80">
        <template v-slot="{ row }">
          <el-tag class="ml-2" type="success">
            <a :href="row.href" target="_blank">文件链接</a>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发布日期" align="center" prop="publishDate" width="180"/>
      <el-table-column label="来源" align="center" prop="source" width="200"/>
      <el-table-column label="是否重点关注" align="center" prop="isImportant">
        <template v-slot="{ row }">
          <span v-if="row.isImportant === 0">否</span>
          <span v-else-if="row.isImportant === 1">是</span>
        </template>
      </el-table-column>

    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @update:page="getList"
        @update:limit="getList"
    />
    <!-- 添加或修改文件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题"/>
        </el-form-item>
        <el-form-item label="链接" prop="href">
          <el-input v-model="form.href" placeholder="请输入链接"/>
        </el-form-item>
        <el-form-item label="发布日期" prop="publishDate">
          <el-date-picker clearable
                          v-model="form.publishDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择发布日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="来源" prop="source">
          <el-input v-model="form.source" placeholder="请输入来源"/>
        </el-form-item>
        <el-form-item label="是否重点关注" prop="isImportant">
          <el-input v-model="form.isImportant" placeholder="请输入是否重点关注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDocuments,
  getDocuments,
  delDocuments,
  addDocuments,
  updateDocuments,
  queryPolicy
} from "@/api/system/documents";

export default {
  name: "Documents",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件表格数据
      documentsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        href: null,
        publishDate: null,
        source: null,
        isImportant: null,
        municipal_level: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          {required: true, message: "标题不能为空", trigger: "blur"}
        ],
        href: [
          {required: true, message: "链接不能为空", trigger: "blur"}
        ],
        publishDate: [
          {required: true, message: "发布日期不能为空", trigger: "blur"}
        ],
        source: [
          {required: true, message: "来源不能为空", trigger: "blur"}
        ],
      },
      provincial_level: [],
      municipal_level: [],
    };
  },
  created() {
    const {proxy} = getCurrentInstance();
    const {municipal_level, provincial_level,} = proxy.useDict("municipal_level", "provincial_level");
    this.municipal_level = municipal_level
    this.provincial_level = provincial_level
    this.getList();
  },
  methods: {
    /** 查询文件列表 */
    getList() {
      if (this.queryParams.municipal_level && this.queryParams.municipal_level !== '') {
        this.queryParams.source = this.queryParams.municipal_level
      }
      this.loading = true;
      listDocuments(this.queryParams).then(response => {
        this.documentsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        href: null,
        publishDate: null,
        source: null,
        isImportant: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDocuments(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文件";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDocuments(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDocuments(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除文件编号为"' + ids + '"的数据项？').then(function () {
        return delDocuments(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/documents/export', {
        ...this.queryParams
      }, `documents_${new Date().getTime()}.xlsx`)
    },
    // 获取数据
    queryPolicy() {
      queryPolicy().then(res => {
        this.$message.success("获取成功")
      })
    }
  }
};
</script>
